package com.app.controller;

import static com.app.util.StringUtilsEx.isEmpty;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.app.bean.Movies;
import com.app.bean.Users;
import com.app.bean.UsersExample;
import com.app.bean.UsersExample.Criteria;
import com.app.core.ReturnVoCommon;
import com.app.mapper.UsersMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UsersMapper userMapper;
	
	@RequestMapping("/list")
	@ResponseBody
	public Map<String,Object> list(Integer page,Integer rows){
		
		Map<String,Object> result = new HashMap<>();
		List<Users> list = null;
		try {
			
			if(isEmpty(page) || isEmpty(rows)) {
				page = 1;
				rows = 5;
			}
			
			PageHelper.startPage(page, rows);
			
			list = userMapper.selectByExample(new UsersExample());
			
			result.put("rows", list);
			PageInfo<Users> pageInfo = new PageInfo<Users>(list);
			result.put("total", pageInfo.getTotal());
			
		} catch (Exception e) {
			
			e.printStackTrace();
			return null;
		}
		
		return result;
		
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public Map<String,Object> delete(String id){
		Map<String,Object> returnVo = new HashMap<String, Object>();
		returnVo.put("code", 0);
		
		try{
			userMapper.deleteByPrimaryKey(Integer.parseInt(id));
		}catch(Exception e){
			returnVo.put("code", -1);
		}
		
		return returnVo;
	}
	
	@RequestMapping("/reg")
	@ResponseBody
	public ReturnVoCommon reg(Users user,HttpServletRequest request){
		
		ReturnVoCommon vo = new ReturnVoCommon();
		
		UsersExample example = new UsersExample();
		Criteria createCriteria = example.createCriteria();
		createCriteria.andNameEqualTo(user.getName());
		List<Users> list = userMapper.selectByExample(example);
		
		if(list.size() == 1){
			vo.setCode(-1);
			vo.setErrMsg("用户名存在！");
			return vo;
		}
		
		user.setAuth("注册用户");
		userMapper.insert(user);
		
		return vo;
	}
	
	
	@RequestMapping("/login")
	@ResponseBody
	public ReturnVoCommon login(Users user,HttpServletRequest request){
		ReturnVoCommon vo = new ReturnVoCommon();
		
		UsersExample example = new UsersExample();
		Criteria createCriteria = example.createCriteria();
		createCriteria.andNameEqualTo(user.getName());
		createCriteria.andPasswordEqualTo(user.getPassword());
		createCriteria.andAuthEqualTo(user.getAuth());
		
		List<Users> list = userMapper.selectByExample(example);
		
		if(list.size() < 1){
			vo.setCode(-1);
			vo.setErrMsg("用户名或者密码错误！");
			return vo;
		}
		
		
		if(user.getAuth().equals("注册用户")){
			vo.setCode(1);
			request.getSession().setAttribute("currentUser", list.get(0));
		}else{
			vo.setCode(2);
			request.getSession().setAttribute("adminUser", list.get(0));
		}
		
		return vo;
	}

}

